package cn.aib.orderservice.controller;


import cn.aib.commonutils.R;
import cn.aib.commonutils.vo.OrderVo;
import cn.aib.orderservice.entity.Order;
import cn.aib.orderservice.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-23
 */
@RestController
@RequestMapping("/orderservice/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     *  生成订单
     *  方法名：createOrder
     *  参数：订单实体类
     *  返回值：R
     */
    @PostMapping("createOrder")
    private R createOrder(@RequestBody Order order){
        System.out.println("order===============:"+order);
        String orderNumber = orderService.createOrder(order);

        return R.ok().data("orderNum",orderNumber);
    }

    /**
     *  查询订单
     *  方法名：getOrderInfo
     *  参数：订单实体类
     *  返回值：R
     */
    @GetMapping("getOrderInfo/{orderNum}")
    private R getOrderInfo(@PathVariable String orderNum){

        Order order = orderService.getOrderInfo(orderNum);

        return R.ok().data("order",order);
    }

    /**
     *  查询订单
     *  方法名：getOrderInfo
     *  参数：订单实体类
     *  返回值：R
     */
    @GetMapping("/getOrderInfoToPay/{orderNum}")
    private OrderVo getOrderVo(@PathVariable("orderNum") String orderNum){

        Order order = orderService.getOrderInfo(orderNum);

        OrderVo orderVo = new OrderVo();

        BeanUtils.copyProperties(order, orderVo);

        return orderVo;
    }

    /**
     *  修改订单状态
     *  方法名：getOrderInfo
     *  参数：订单实体类
     *  返回值：R
     */
    @PostMapping("/updateOrderStatus")
    private R updateOrderStatus(@RequestBody OrderVo orderVo){

        Order order = new Order();

        BeanUtils.copyProperties(orderVo,order);

        boolean flag = orderService.updateById(order);

        if(!flag){
            return R.error();
        }

        return R.ok();
    }

    /**
     *  查询订单是否支付
     *  方法名：isOrderByPay
     *  参数：课程ID，用户ID
     *  返回值：R
     */
    @GetMapping("/isOrderByPay/{courseId}/{MemberId}")
    private Boolean isOrderByPay(@PathVariable String courseId,@PathVariable String MemberId){

        //查询订单是否支付成功
        QueryWrapper<Order> wrapper = new QueryWrapper<>();

        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", MemberId);
        wrapper.eq("status", 1);

        int count = orderService.count(wrapper);

        return count>0?true:false;
    }
}